package org.pt.config;


import io.swagger.v3.oas.models.*;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import io.swagger.v3.oas.models.media.*;
import io.swagger.v3.oas.models.responses.ApiResponse;
import io.swagger.v3.oas.models.responses.ApiResponses;
import org.pt.components.Response;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;



@Configuration
public class SpringDocConfig {
    @Bean
    public OpenAPI myOpenAPI() {
        return new OpenAPI()
                .components(new Components()
                        .addSchemas("GlobalResponse", new ObjectSchema()
                                .addProperty("code", new IntegerSchema().example(200))
                                .addProperty("msg", new StringSchema().example("success"))
                                .addProperty("data", new ObjectSchema().nullable(true))
                        )
                )
                .info(new Info()
                        .title("myPT API")
                        .description("接口文档")
                        .version("v1.0.0")
                        .contact(new Contact()
                                .name("hqz")
                                .email("2462546443@qq.com")))
                .paths(new Paths()
                        .addPathItem("/user", new PathItem()
                                .get(new Operation()
                                        .summary("用户部分错误码")
                                        .description("用户部分可能出现的错误码，具体内容见实习返回的msg")
                                        .responses(new ApiResponses()
                                                // 自定义业务错误码（3xx系列）
                                                .addApiResponse("301", createErrorResponse("TOKEN验证失败"))
                                                .addApiResponse("302", createErrorResponse("TOKEN过期"))
                                                .addApiResponse("303", createErrorResponse("账号已在其他地方登录"))

                                                // 邀请相关（4xx系列）
                                                .addApiResponse("401", createErrorResponse("邀请人存在"))

                                                // 认证授权相关（5xx系列）
                                                .addApiResponse("502", createErrorResponse("授权失败"))
                                                .addApiResponse("503", createErrorResponse("登陆错误"))
                                                .addApiResponse("504", createErrorResponse("注册错误"))
                                                .addApiResponse("505", createErrorResponse("用户错误"))
                                        )
                                )
                        ));
    }
    private ApiResponse createErrorResponse(String description) {
        return new ApiResponse()
                .description(description)
                .content(new Content()
                        .addMediaType(
                                MediaType.APPLICATION_JSON_VALUE,
                                new io.swagger.v3.oas.models.media.MediaType()
                                        .schema(new Schema<Response>()
                                                .$ref("#/components/schemas/GlobalResponse")
                                        )
                        )
                );
}}

